package com.vcc.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.service.IService;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.vcc.data.entity.ShareAccountConfig;
import com.vcc.data.mapper.ShareAccountConfigMapper;
import com.vcc.service.boss.share.config.request.BossShareAccountConfigUpdateRequest;
import com.vcc.service.boss.share.config.response.BossShareAccountConfigResponse;
import com.vcc.service.service.ShareAccountConfigService;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

import static com.vcc.data.entity.table.CardTypeTableDef.CARD_TYPE;
import static com.vcc.data.entity.table.ShareAccountConfigTableDef.SHARE_ACCOUNT_CONFIG;
import static com.vcc.data.entity.table.ShareAccountTableDef.SHARE_ACCOUNT;

@Service
public class ShareAccountConfigServiceImpl extends ServiceImpl<ShareAccountConfigMapper,ShareAccountConfig> implements ShareAccountConfigService {
    @Override
    public List<BossShareAccountConfigResponse> listByUserId(Long userId) {
        QueryChain<ShareAccountConfig> queryChain = queryChain();
        queryChain.select(SHARE_ACCOUNT_CONFIG.ALL_COLUMNS);
        queryChain.select(CARD_TYPE.SECTION_NO.as("sectionNo"));
        queryChain.select(CARD_TYPE.CHANNEL_NAME.as("channelCode"));
        queryChain.select(SHARE_ACCOUNT.ACCOUNT_NAME.as("shareAccountName"));
        queryChain.leftJoin(CARD_TYPE).on(SHARE_ACCOUNT_CONFIG.CARD_TYPE_ID.eq(CARD_TYPE.ID));
        queryChain.leftJoin(SHARE_ACCOUNT).on(SHARE_ACCOUNT_CONFIG.SHARE_ACCOUNT_ID.eq(SHARE_ACCOUNT.ID));
        queryChain.where(SHARE_ACCOUNT_CONFIG.USER_ID.eq(userId));
        return queryChain.listAs(BossShareAccountConfigResponse.class);
    }

    @Override
    public List<ShareAccountConfig> listByUserIds(List<Long> userIds) {
        return queryChain()
                .where(SHARE_ACCOUNT_CONFIG.USER_ID.in(userIds))
                .list();
    }

    @Override
    public Boolean update(BossShareAccountConfigUpdateRequest request) {
        ShareAccountConfig shareAccountConfig = getById(request.getId());
        UpdateChain<ShareAccountConfig> updatedChain = updateChain();
        if (request.getAmount() != null && request.getAmount().compareTo(shareAccountConfig.getAmount()) != 0 && request.getAmount().compareTo(BigDecimal.valueOf(-1)) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.AMOUNT, request.getAmount());
        }
        if (request.getVerifyOpenCardStatus() != null && !request.getVerifyOpenCardStatus().equals(shareAccountConfig.getVerifyOpenCardStatus())) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.VERIFY_OPEN_CARD_STATUS, request.getVerifyOpenCardStatus());
        }
        if (request.getVerifyUpdateLimitStatus() != null && !request.getVerifyUpdateLimitStatus().equals(shareAccountConfig.getVerifyUpdateLimitStatus())) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.VERIFY_UPDATE_LIMIT_STATUS, request.getVerifyUpdateLimitStatus());
        }
        if (request.getVerifyCloseCardStatus() != null && !request.getVerifyCloseCardStatus().equals(shareAccountConfig.getVerifyCloseCardStatus())) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.VERIFY_CLOSE_CARD_STATUS, request.getVerifyCloseCardStatus());
        }
        if (request.getVerifyFreezeUnFreezeCardStatus() != null && !request.getVerifyFreezeUnFreezeCardStatus().equals(shareAccountConfig.getVerifyFreezeUnFreezeCardStatus())) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.VERIFY_FREEZE_UN_FREEZE_CARD_STATUS, request.getVerifyFreezeUnFreezeCardStatus());
        }
        if (request.getAuthFee() != null && request.getAuthFee().compareTo(shareAccountConfig.getAuthFee()) != 0 && request.getAuthFee().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.AUTH_FEE, request.getAuthFee());
        }
        if (request.getAuthFeeRate() != null && request.getAuthFeeRate().compareTo(shareAccountConfig.getAuthFeeRate()) != 0 && request.getAuthFeeRate().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.AUTH_FEE_RATE, request.getAuthFeeRate());
        }
        if (request.getMinAuthFee() != null && request.getMinAuthFee().compareTo(shareAccountConfig.getMinAuthFee()) != 0 && request.getMinAuthFee().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.MIN_AUTH_FEE, request.getMinAuthFee());
        }
        if (request.getMinAuthFeeRate() != null && request.getMinAuthFeeRate().compareTo(shareAccountConfig.getMinAuthFeeRate()) != 0 && request.getMinAuthFeeRate().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.MIN_AUTH_FEE_RATE, request.getMinAuthFeeRate());
        }
        if (request.getFailFee() != null && request.getFailFee().compareTo(shareAccountConfig.getFailFee()) != 0 && request.getFailFee().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.FAIL_FEE, request.getFailFee());
        }
        if (request.getFailFeeRate() != null && request.getFailFeeRate().compareTo(shareAccountConfig.getFailFeeRate()) != 0 && request.getFailFeeRate().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.FAIL_FEE_RATE, request.getFailFeeRate());
        }
        if (request.getRefundFee() != null && request.getRefundFee().compareTo(shareAccountConfig.getRefundFee()) != 0 && request.getRefundFee().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.REFUND_FEE, request.getRefundFee());
        }
        if (request.getRefundFeeRate() != null && request.getRefundFeeRate().compareTo(shareAccountConfig.getRefundFeeRate()) != 0 && request.getRefundFeeRate().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.REFUND_FEE_RATE, request.getRefundFeeRate());
        }
        if (request.getReversalFee() != null && request.getReversalFee().compareTo(shareAccountConfig.getReversalFee()) != 0 && request.getReversalFee().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.REVERSAL_FEE, request.getReversalFee());
        }
        if (request.getReversalFeeRate() != null && request.getReversalFeeRate().compareTo(shareAccountConfig.getReversalFeeRate()) != 0 && request.getReversalFeeRate().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.REVERSAL_FEE_RATE, request.getReversalFeeRate());
        }
        if (request.getCrossBorderFee() != null && request.getCrossBorderFee().compareTo(shareAccountConfig.getCrossBorderFee()) != 0 && request.getCrossBorderFee().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.CROSS_BORDER_FEE, request.getCrossBorderFee());
        }
        if (request.getCrossBorderFeeRate() != null && request.getCrossBorderFeeRate().compareTo(shareAccountConfig.getCrossBorderFeeRate()) != 0 && request.getCrossBorderFeeRate().compareTo(BigDecimal.ZERO) >= 0) {
            updatedChain.set(SHARE_ACCOUNT_CONFIG.CROSS_BORDER_FEE_RATE, request.getCrossBorderFeeRate());
        }
        updatedChain.where(SHARE_ACCOUNT_CONFIG.ID.eq(request.getId()));
        return updatedChain.update();
    }

    @Override
    public ShareAccountConfig getByUserIdAndCardTypeId(Long userId, Long cardTypeId) {
        return queryChain()
                .where(SHARE_ACCOUNT_CONFIG.USER_ID.eq(userId))
                .and(SHARE_ACCOUNT_CONFIG.CARD_TYPE_ID.eq(cardTypeId))
                .oneOpt()
                .orElse(null);
    }
}
